package com.dwxt.sport.service;

import com.dwxt.sport.dao.DeviceSceneMapper;
import com.dwxt.sport.dao.WifeDeviceMapper;
import com.dwxt.sport.entity.DeviceScene;
import com.dwxt.sport.entity.WifeDevice;
import com.dwxt.sport.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WifeDeviceService {
    @Autowired
    private WifeDeviceMapper wifeDeviceMapper;

    @Autowired
    private DeviceSceneMapper deviceSceneMapper;

    public List<WifeDevice> selectByType(int type) {
        Example example = new Example(WifeDevice.class);
        example.createCriteria().andEqualTo("type", type).andEqualTo("isActive", 1);
        return wifeDeviceMapper.selectByExample(example);
    }

    public List<WifeDevice> selectDevicesBySceneId(String id) {
        List<DeviceScene> deviceSceneList = getDeviceScenesBySceneId(id);
        List<String> collect = deviceSceneList.stream().map(DeviceScene::getDeviceId).collect(Collectors.toList());
        List<WifeDevice> wifeDeviceList = getWifeDevicesByIds(collect);
        return wifeDeviceList;
    }

    public List<WifeDevice> getWifeDevicesByIds(List<String> collect) {
        Example example = new Example(WifeDevice.class);
        example.createCriteria().andIn("id", collect).andEqualTo("type", 1);
        return wifeDeviceMapper.selectByExample(example);
    }

    public List<DeviceScene> getDeviceScenesBySceneId(String id) {
        Example example = new Example(DeviceScene.class);
        example.createCriteria().andEqualTo("sceneId", id);
        return deviceSceneMapper.selectByExample(example);
    }
}
